package com.example.wms;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.wms.utils.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity2 extends AppCompatActivity {

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private EditText input1;
    private EditText input2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        input1 = findViewById(R.id.input1);
        input2 = findViewById(R.id.input2);
        btn1 = findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("wwwwwww");
                startActivityForResult(new Intent(MainActivity2.this, QRCodeActivity.class), 1);
            }
        });
        btn2 = findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(MainActivity2.this, QRCodeActivity.class), 2);

            }
        });
        btn3 = findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sharedPreferences = getSharedPreferences("cc", Context.MODE_PRIVATE);
                String order_id = sharedPreferences.getString("order_id", null);
                String pid = sharedPreferences.getString("p_id", null);
                String extract_id = sharedPreferences.getString("extract_id", null);
                System.out.println(order_id+" pid "+pid+"extract_id"+extract_id);

                System.out.println(input1.getText().toString() +"##"+input2.getText().toString());
                if (!input1.getText().toString().equals(pid)) {
                    Toast.makeText(MainActivity2.this, "商品匹配错误，请重试", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!input2.getText().toString().equals(extract_id)) {

                    Toast.makeText(MainActivity2.this, "提取码错误，请重试", Toast.LENGTH_SHORT).show();
                    return;
                }
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("id", order_id);
                    jsonObject.put("pid", pid);
                    jsonObject.put("auditFlag", 3);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                OkHttpUtils.getInstance().extract(jsonObject, new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        System.out.println("请求失败");
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        System.out.println("请求成功");
                        System.out.println(response.body().toString());
                        finish();
                    }
                });

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            String code = data.getStringExtra("value");
            if (requestCode == 1) {
                input1.setText(code + "");
            } else {
                input2.setText(code + "");
            }
            System.out.println(code + "value:" + requestCode);
        }
    }
}